package hc.um.action;

import hc.tool.UtilityServiec;
import hc.um.Employee;
import hc.um.service.UserService;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterUserServlet
 */
@WebServlet("/RegisterUserServlet")
public class RegisterUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private UserService userService = new UserService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//System.out.println("user=" + request.getParameter("username"));
		//System.out.println("pwd=" + request.getParameter("password"));
		try {
			this.registerUser(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	public void registerUser(HttpServletRequest request,
			HttpServletResponse response) throws IOException, SQLException {
		Employee employee = new Employee();
		
		String userid = null;
		
		if( (userid = generateUserId(Integer.parseInt(request.getParameter("roleType")))) == null )
		{
			response.getWriter().write(creatXmlTextRegisterUser("Fail","Gererate user failed!",""));
			return;
		}
		
		employee.setID(userid);
		employee.setPwd("123456");
		employee.setLastName(request.getParameter("lname"));
		employee.setFirstName(request.getParameter("fname"));
		employee.setBirthday(request.getParameter("birthday"));
		employee.setSex(request.getParameter("sexType"));
		employee.setPhone(request.getParameter("phone"));
		employee.setEmail(request.getParameter("email"));
		employee.setAddress(request.getParameter("address"));
		employee.setHospitalid(Integer.parseInt(request.getParameter("hospitalType")));
		employee.setRoleID(Integer.parseInt(request.getParameter("roleType")));
		
		if ( userService.addUser(employee) == 1 )
		{
			response.getWriter().write(creatXmlTextRegisterUser("Success","Registered user successfully!",userid));
		} 
		else 
		{
			response.getWriter().write(creatXmlTextRegisterUser("Fail","Registered user failed.",""));
		}
	}
	
	public String creatXmlTextRegisterUser(String result, String message, String userid) {
		String xmltext = null;

		xmltext = "<registeruser><result>" + result + "</result><message>"
				+ message + "</message><userid>" + userid + "</userid></registeruser>";
		//System.out.println("xmltext="+xmltext);
		
		return xmltext;
	}	
	
	public String generateUserId(int roleid)
	{
//		int sequence;
//		String wuserid="";
//		
//		if( (sequence = userService.getUserIdSequence(roleid)) != -1 )
//		{
//			switch (roleid)
//			{
//				case 1:
//				case 2:
//					wuserid += "OFC";
//					break;
//				case 3:
//					wuserid += "DOC";
//					break;
//				case 4:
//					wuserid += "AST";
//					break;
//				case 5:
//					wuserid += "PAT";
//					break;	
//				default:
//				{
//					System.out.println("roleid=" + roleid + " is invalid!");
//					return null;
//				}
//			}
//					
//			wuserid += String.format("%06d",sequence);			
//			
//            if( (sequence + 1) > 99999 )          //assume the max value of user sequence No is 999999
//            	sequence = 1;
//            else
//            	sequence += 1;	
//            
//			if( userService.updateUserIdSequence(roleid,sequence) != 1 )
//			{
//				System.out.println("update user sequence failed. roleid="+roleid);
//				wuserid = "";
//				return null;
//			}
//			
//			System.out.println("user id =" + wuserid);
//		}
		//return wuserid;	
		
		return UtilityServiec.getUserID(roleid);

	}
}
